package burubaka.controller.mnt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import net.arnx.jsonic.JSON;

import org.slim3.controller.Navigation;

import burubaka.common.controller.Controller;
import burubaka.model.RaceInfo;
import burubaka.service.RaceInfoService;
import burubaka.service.ResultHorseService;

public class ResultController extends Controller {

	private static final Logger log = Logger.getLogger(NominationController.class.getName());

	private RaceInfoService raceInfoService = new RaceInfoService();
	private ResultHorseService resultHorseService = new ResultHorseService();

	/**
	 * 検索
	 * 
	 * @return Navigation
	 * @throws Exception
	 */
	public Navigation search() throws Exception {

		Map<String, Object> resMap = new HashMap<String, Object>();
		// String proc = (String)reqMap.get("proc");
		String raceKey = (String) reqMap.get("raceKey");
		RaceInfo raceInfo = raceInfoService.getRaceInfoByRaceDate(raceKey);

		List<?> list = resultHorseService.getResultHorseList(raceInfo);
		// if (BurubakaUtil.isEmpty(list)) {
		// list = raceCardHorseService.getRaceCardHorseList(raceInfo);
		// }

		resMap.put("raceInfo", raceInfo);
		resMap.put("rsHorseL", list);

		String jsonText = JSON.encode(resMap, true);

		log.info(jsonText);

		response.setContentType("application/json; charset=UTF-8");
		response.getWriter().write(jsonText);
		response.getWriter().flush();

		return null;
	}

	/**
	 * 登録
	 * 
	 * @return Navigation
	 * @throws Exception
	 */
	public Navigation regist() throws Exception {
		// Map<String, Object> map =
		// JSON.decode((String)input.get("json"),HashMap.class);
		// String raceDate = (String)map.get("raceDate");
		String raceDate = (String) reqMap.get("raceDate");
		RaceInfo raceInfo = raceInfoService.getRaceInfoByRaceDate(raceDate);
		resultHorseService.regist(reqMap, raceInfo);
		return null;
	}
}